package com.zzptc.sgz.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText et_sgz_name,et_sgz_pwd;
    Switch aSwitch_sgz;
    Button btn_sgz_login;
    boolean flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        eventListener();
    }

    private void eventListener() {
        //自动登陆是否改变
        aSwitch_sgz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    flag=true;
                }else{
                    flag=false;
                }
                Toast.makeText(MainActivity.this, ""+isChecked, Toast.LENGTH_SHORT).show();
            }
        });

        btn_sgz_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户名和密码的值
                String name = et_sgz_name.getText().toString().trim();
                String pwd = et_sgz_pwd.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("one", MODE_PRIVATE);
                SharedPreferences.Editor edit = sh.edit();
                if(flag){
                    //记住（用户名、密码、是否自动登陆）
                    edit.putString("uname",name);
                    edit.putString("password", pwd);
                    edit.putBoolean("autologin",flag);
                    edit.commit();
                }else{
                    edit.putString("uname","");
                    edit.putString("password", "");
                    edit.putBoolean("autologin",false);
                    edit.commit();
                }

                //登陆
                login(name,pwd);
            }
        });
    }
    public void login(String username,String password){

    }
    private void init() {
        et_sgz_name=findViewById(R.id.et_sgz_name);
        et_sgz_pwd=findViewById(R.id.et_sgz_pwd);
        aSwitch_sgz=findViewById(R.id.switch_auto_login);
        btn_sgz_login=findViewById(R.id.btn_sgz_login);


    }
}
